<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button
            type="primary"
            size="small"
            @click="addclick"
          >+ 新增角色</el-button>

          <el-table border :data="list">
            <el-table-column label="序号" type="index" width="180px">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="PermissionClick(row)"
                  >分配权限</el-button>
                  <el-button
                    type="text"
                    @click="EditCilck(row)"
                  >修改</el-button>
                  <el-button type="text" @click="DelClick(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 2, 3, 4,5, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司设置">
          <Info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 新增角色 -->
    <Add ref="add" :is-show="show" @close="close" @getData="getData" />
    <!-- 分配权限 -->
    <setPermission ref="setpermission" />
  </div>
</template>
<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import Add from './components/add.vue'
import setPermission from './components/setPermission.vue'
export default {
  name: '',
  components: {
    Add,
    setPermission,
    Info: () => import('./components/info.vue')
  },
  data() {
    return {
      list: [],
      show: false,
      page: {
        page: 1,
        pagesize: 5,
        total: 100
      }
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(size) {
      console.log(size)
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      console.log(page)
      this.page.page = page
      this.getData()
    },
    // 获取所以角色
    async getData() {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log(res)
    },
    // 点击新增按钮
    addclick() {
      this.show = true
      this.$refs.add.lookShow = true
      this.$refs.add.mode = 'add'
    },
    close() {
      this.show = false
    },
    // 点击删除
    DelClick(row) {
      this.$confirm('您确定要删除该角色吗？', '提示')
        .then(async() => {
          await sysRoleDel(row.id)
          this.$message.success('删除成功')
          if (this.page.page > 1 && this.list.length === 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    EditCilck(row) {
      this.show = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    PermissionClick(row) {
      this.$refs.setpermission.isShow = true
      this.$refs.setpermission.getRoleInfo(row)
    }
  }
}
</script>
<style lang="scss" scoped>
.setting {
  padding: 15px;
}
</style>
